﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication4
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var name = TextBox1.Text.Trim();
            var pwd = TextBox2.Text.Trim();
            var sql = string.Format("select * from Users where UserName='{0}'", name);
            var dt= DbHelper.GetData(sql);
            if (dt.Rows.Count == 1)
            {
                var str = dt.Rows[0];
                var pass = str.ItemArray[2].ToString();
                var isInvoke = (bool)str.ItemArray[3];
                var isDelete = (bool)str.ItemArray[4];
                if (pass == pwd)
                {
                    if (isDelete)
                    {
                        Response.Write("用户已被删除");
                    }
                    else
                    {
                        if (isInvoke)
                        {
                            Response.Redirect("default.aspx");
                            Response.Write("登入成功");
                        }
                        else
                        {
                            Response.Write("用户已被禁止");
                        }
                    }
                }
                else
                {
                    Response.Write("用户密码错误");
                }
             }
            else
            {
                Response.Write("用户不存在");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var name = TextBox1.Text.Trim();
            var pwd = TextBox2.Text.Trim();
            var sql = string.Format("select * from Users where UserName='{0}'", name);
            var dt = DbHelper.GetData(sql);
            if (dt.Rows.Count == 1)
            {
                Response.Write("用户名已存在");
            }
            else
            {
                if (name == "" || pwd == "")
                {
                    Response.Write("不能为空值");
                }
                else
                {
                    Response.Write("注册成功");
                    var sqlStr = string.Format("insert into Users(UserName,UserPassword)values('{0}','{1}')", name, pwd);
                    DbHelper.ADU(sqlStr);
                }
               
            }
        }
    }
}